#!/bin/bash

# 获取当前 Git 分支名称
BRANCH=$(git rev-parse --abbrev-ref HEAD)

# 获取当前 Git commit ID
COMMIT_ID=$(git rev-parse --short HEAD)

# 获取当前 Git commit ID
DESC=$(git describe always)

# 获取当前时间
BUILD_TIME=$(date "+%Y-%m-%d %H:%M:%S")

echo "正在清空 dist 目录..."
rm -rf dist/*
# 执行 vue 构建命令
echo "正在执行打包..."
npm run build:mobile
# 检查 dist 目录是否存在
if [ ! -d "dist" ]; then
  echo "错误：dist 目录未找到"
  exit 1
fi

# 创建 version 文件
echo "正在创建 version.json 文件..."
echo '{
  "branch": "'$BRANCH'",
  "commitId": "'$COMMIT_ID'",
  "describe": "'$DESC'",
  "buildTime": "'$BUILD_TIME'"
}' > "dist/version.json"
echo "version.json 文件已创建"

# 使用 zip 压缩 dist 目录
echo "正在压缩 dist 目录..."
cd dist
tar -cf "YM-801S-OSCS-V$COMMIT_ID-FE.zip" .
echo "dist 目录已压缩为 YM-801S-OSCS-V$COMMIT_ID-FE.zip"
